home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-24 | 3.2 KB | 116 lines | [TEXT/CWIE] |
- // ----------------------------------------------------------------------------
- // dynarray.t
- // ----------------------------------------------------------------------------
- // By Jean-François Brouillet, Copyright © 1995
- //
- // V0.000 951023 JFB Creation
- //
- // No restriction on use. But please, use with caution as this is not
- // thoroughly tested.
-
- #ifndef __DYNARRAY__
- #define __DYNARRAY__
-
- #include <new>
- #include <string.h>
- #include <exception>
-
- // ----------------------------------------------------------------------------
- // template dynarray
- // ----------------------------------------------------------------------------
- template <class T>
- class dynarray
- {
- public:
- enum
- {
- kDefaultExtraSlots = 16
- };
- // ----------------------------------------------------------------------------
- // dynarray::dynarray
- // ----------------------------------------------------------------------------
- dynarray( const size_t inExtraSlots = kDefaultExtraSlots)
- : m_Array(0)
- , m_NumElems(0)
- , m_FreeSlots(0)
- , m_MaxFreeSlots(0)
- {
- m_Array = new T [inExtraSlots];
-
- if (m_Array)
- {
- m_NumElems = 0;
- m_FreeSlots = inExtraSlots;
- m_MaxFreeSlots = inExtraSlots;
- }
- else
- throw xalloc();
- };
-
- // ----------------------------------------------------------------------------
- // dynarray::~dynarray
- // ----------------------------------------------------------------------------
- ~dynarray()
- {
- delete [] m_Array;
- };
-
- // ----------------------------------------------------------------------------
- // dynarray::resize
- // ----------------------------------------------------------------------------
- inline void resize(const size_t inNewElementCount)
- {
- if (inNewElementCount > m_NumElems + m_FreeSlots)
- {
- T * newArray = new T [inNewElementCount + m_MaxFreeSlots];
-
- if (newArray)
- {
- memcpy(newArray, m_Array, sizeof(T) * m_NumElems);
- delete [] m_Array;
- m_Array = newArray;
- m_NumElems = inNewElementCount;
- m_FreeSlots = m_MaxFreeSlots;
- }
- else
- throw xalloc();
- }
- else if (inNewElementCount > m_NumElems)
- {
- m_FreeSlots -= inNewElementCount - m_NumElems;
- m_NumElems = inNewElementCount;
- }
- else
- {
- m_FreeSlots += m_NumElems - inNewElementCount;
- m_NumElems = inNewElementCount;
- }
- };
-
- // ----------------------------------------------------------------------------
- // dynarray::operator []
- // ----------------------------------------------------------------------------
- inline T & operator [] (const size_t inIndex)
- {
- if (inIndex >= m_NumElems)
- throw outofrange();
- return m_Array[inIndex];
- };
-
- // ----------------------------------------------------------------------------
- // dynarray::count
- // ----------------------------------------------------------------------------
- inline size_t count(void)
- {
- return m_NumElems;
- };
-
- protected:
- T * m_Array;
- size_t m_NumElems;
- size_t m_FreeSlots;
- size_t m_MaxFreeSlots;
- };
-
-
- #endif /* __DYNARRAY__ */